﻿using Microsoft.Extensions.FileProviders;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection.Emit;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Data;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Domain.Repositories;
using Newtonsoft.Json;

namespace Himp.Platform
{
    public class DataDictDataSeederContributor : IDataSeedContributor, ITransientDependency
    {
        private readonly IRepository<DataDict, Guid> _dataDictionaryRepository;
        private readonly IRepository<CatgeDict, Guid> _dataDictionaryTypeRepository;

        public DataDictDataSeederContributor(IRepository<DataDict, Guid> dataDictionaryRepository,
            IRepository<CatgeDict, Guid> dataDictionaryTypeRepository)
        {
            _dataDictionaryRepository = dataDictionaryRepository;
            _dataDictionaryTypeRepository = dataDictionaryTypeRepository;
        }

        private const string datas = @"
[
{
  ""DataDictTypeCode"": ""nat_regn_code"",
  ""DataDictTypeName"": ""国家地区代码"",
  ""DataDictTypeModule"": 0,
  ""Datas"": [
    {
      ""Code"": ""CHN"",
      ""Name"": ""中国""
    }
  ]
},
{
  ""DataDictTypeCode"": ""caty"",
  ""DataDictTypeName"": ""科别"",
  ""DataDictTypeModule"": 0,
  ""Datas"": [
    {
      ""Code"": ""A01"",
      ""Name"": ""预防保健科""
    }
  ]
},
{
  ""DataDictTypeCode"": ""naty"",
  ""DataDictTypeName"": ""民族"",
  ""DataDictTypeModule"": 0,
  ""Datas"": [
    {
      ""Code"": ""01"",
      ""Name"": ""汉族""
    }
  ]
},
{
  ""DataDictTypeCode"": ""prfs"",
  ""DataDictTypeName"": ""职业"",
  ""DataDictTypeModule"": 0,
  ""Datas"": [
    {
      ""Code"": ""90"",
      ""Name"": ""其他""
    }
  ]
},
{
  ""DataDictTypeCode"": ""psn_cert_type"",
  ""DataDictTypeName"": ""人员证件类型"",
  ""DataDictTypeModule"": 0,
  ""Datas"": [
    {
      ""Code"": ""01"",
      ""Name"": ""居民身份证（户口簿）""
    },
    {
      ""Code"": ""02"",
      ""Name"": ""中国人民解放军军官证""
    }
  ]
},
{
  ""DataDictTypeCode"": ""member_source"",
  ""DataDictTypeName"": ""会员来源"",
  ""DataDictTypeModule"": 0,
  ""Datas"": [
    {
      ""Code"": ""99"",
      ""Name"": ""其它""
    }
  ]
},
{
  ""DataDictTypeCode"": ""member_level"",
  ""DataDictTypeName"": ""会员等级"",
  ""DataDictTypeModule"": 0,
  ""Datas"": [
    {
      ""Code"": ""99"",
      ""Name"": ""其它""
    }
  ]
}
]";


        #region 药品相关

        //使用频次（used_frqu_type）
        public const string USED_FRQU_TYPE_CODE = "used_frqu_type";
        public const string USED_FRQU_TYPE_NAME = "使用频次类别";

        #endregion


        public async Task SeedAsync(DataSeedContext context)
        {
            await LoadJsonFile();
        }

        /// <summary>
        /// 加载Json文件
        /// </summary>
        private async Task LoadJsonFile()
        {
            //var iDataDictionaryQuery = await _dataDictionaryRepository.GetQueryableAsync();
            //List<DataDictJson> dataDictJsons = JsonConvert.DeserializeObject<List<DataDictJson>>(datas);

            //foreach (var dataDictJson in dataDictJsons)
            //{
            //    var iDataDictionaryTypeQuery = await _dataDictionaryTypeRepository.GetQueryableAsync();

            //    if (!iDataDictionaryTypeQuery.Any(d => d.CatgeCodg == dataDictJson.DataDictTypeCode))
            //    {
            //        DataDictType dataDictionaryType = new DataDictType();
            //        dataDictionaryType.CatgeCodg = dataDictJson.DataDictTypeCode;
            //        dataDictionaryType.CatgeName = dataDictJson.DataDictTypeName;
            //        dataDictionaryType.DataDictTypeModule = dataDictJson.DataDictTypeModule;
            //        await _dataDictionaryTypeRepository.InsertAsync(dataDictionaryType, autoSave: true);
            //    }

            //    iDataDictionaryTypeQuery = await _dataDictionaryTypeRepository.GetQueryableAsync();
            //    var dictionaryType = iDataDictionaryTypeQuery.First(d => d.CatgeCodg == dataDictJson.DataDictTypeCode);
            //    int srtNo = 0;

            //    foreach (var item in dataDictJson.Datas)
            //    {
            //        srtNo++;
            //        if (!iDataDictionaryQuery.Any(d => d.DataDictType.Id == dictionaryType.Id
            //            && d.DataDictCode == item.Code))
            //        {
            //            DataDict dataDictionary = new DataDict();
            //            dataDictionary.DataDictTypeId = dictionaryType.Id;
            //            dataDictionary.DataDictCode = item.Code;
            //            dataDictionary.DataDictName = item.Name;
            //            dataDictionary.SrtNo = srtNo;
            //            await _dataDictionaryRepository.InsertAsync(dataDictionary);
            //        }
            //    }
            //}
        }
    }
}
